<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="奖品号" prop="prizeId">
				<el-input v-model="queryParams.prizeId" placeholder="请输入奖品号" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<router-link :to="{path:'/Info',query:{ safetyExaminationPaperId: $route.query.safetyExaminationId}}">
					<el-button type="primary" icon="el-icon-plus" size="mini">添加奖品</el-button>
				</router-link>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除奖品</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>
		<el-table v-loading="loading" :data="prizeluckList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="奖品号" align="center" prop="prizeId" />
			<el-table-column label="奖品名称" align="center" prop="prizeName" />
			<el-table-column label="奖品数量" align="center" prop="prizeAmount" />
			<el-table-column label="奖品概率" align="center" prop="prizeProbability" />
			<el-table-column label="设置人员" align="center" prop="setId" />
			<el-table-column label="设置时间" align="center" prop="setDate" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:prizeluck:edit']">查看详情</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:prizeluck:remove']">删除奖品</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改奖品抽奖对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="考试号" prop="safetyExaminationId">
					<el-input v-model="form.safetyExaminationId" :disabled="true" placeholder="请输入考试号" />
				</el-form-item>
				<el-form-item label="奖品号" prop="prizeId">
					<el-input v-model="form.prizeId" :disabled="true" placeholder="请输入奖品号" />
				</el-form-item>
				<el-form-item label="奖品名称" prop="prizeName">
					<el-input v-model="form.prizeName" :disabled="true" placeholder="请输入奖品名称" />
				</el-form-item>
				<el-form-item label="奖品数量" prop="prizeAmount">
					<el-input v-model="form.prizeAmount"  placeholder="请输入奖品数量" type="number" :min="0" :max="prizeAmoutmax"/>
					<spqn style="color: #9c9c9c;">ps:剩余奖品数量为{{ form.prizeSurplusAmount }}</spqn>
				</el-form-item>
				<el-form-item label="奖品概率" prop="prizeProbability">
					<el-input v-model="form.prizeProbability" placeholder="请输入奖品概率"  type="number" :min="0" :max="form.prizeSurplusProbability" :step = "0.1"/>
					<spqn style="color: #9c9c9c;">ps:可设置奖品概率为0~{{ form.prizeSurplusProbability }}</spqn>
				</el-form-item>
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="form.setId" :disabled="true" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer" v-if="">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listPrizeluck,
		getPrizeluck,
		delPrizeluck,
		addPrizeluck,
		updatePrizeluck,
		exportPrizeluck,
		listPrizeinfo
	} from "@/api/system/prizeluck";

	export default {
		name: "Prizeluck",
		components: {},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				//奖品最大数
				prizeAmoutmax:0,
				// 奖品抽奖表格数据
				prizeluckList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					safetyExaminationId: null,
					safetyExaminationName: null,
					prizeId: null,
					prizeName: null,
					prizeAmount: null,
					prizeProbability: null,
					setId: null,
					setDate: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					safetyExaminationId: [{
						required: true,
						message: "考试号不能为空",
						trigger: "blur"
					}],
					prizeId: [{
						required: true,
						message: "奖品号不能为空",
						trigger: "blur"
					}],
					prizeName: [{
						required: true,
						message: "奖品名称不能为空",
						trigger: "blur"
					}],
					prizeAmount: [{
						required: true,
						message: "奖品数量不能为空",
						trigger: "blur"
					}],
					prizeProbability: [{
						required: true,
						message: "奖品概率不能为空",
						trigger: "blur"
					}],
					setId: [{
						required: true,
						message: "设置人员不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询奖品抽奖列表 */
			getList() {
				this.loading = true;
				this.queryParams.safetyExaminationId = this.$route.query.safetyExaminationId;
				listPrizeluck(this.queryParams).then(response => {
					this.prizeluckList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					safetyExaminationId: null,
					safetyExaminationName: null,
					prizeId: null,
					prizeName: null,
					prizeAmount: null,
					prizeProbability: null,
					setId: null,
					setDate: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getPrizeluck(id).then(response => {
					console.log(response);
					this.form = response.data;
					this.form.setId = this.$store.getters.userId;
					this.prizeAmoutmax = this.form.prizeAmount + this.form.prizeSurplusAmount;
					this.open = true;
					this.title = "修改奖品抽奖";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							this.form.prizeProbability=Number(this.form.prizeProbability).toFixed(3);
							updatePrizeluck(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$confirm('是否确认删除奖品编号为"' + ids + '"的奖品?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delPrizeluck(ids);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			}
		}
	};
</script>
